package me.wsj.fengyun.dialog

import android.os.Bundle
import android.view.*
import android.view.ViewGroup.LayoutParams
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import android.quickapp.windrain.R

abstract class BaseDialog : DialogFragment() {


    protected var mOnDialogListener: OnDialogListener? = null

    enum class STYLE {
        STYLE_NORMAL(),
        STYLE_BOTTOM(),
        STYLE_LEFT_TOP();
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val style = initDialogStyle()
        if (style == STYLE.STYLE_NORMAL) {
            //正常
            setStyle(STYLE_NORMAL, R.style.DialogTheme)
        } else if (style == STYLE.STYLE_BOTTOM) {
            //底部
            setStyle(STYLE_NORMAL, R.style.DialogAnimBottomTheme)
        } else if (style == STYLE.STYLE_LEFT_TOP) {
            //底部
            setStyle(STYLE_NORMAL, R.style.DialogAnimLeftTopTheme)
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initLayout()
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    override fun onStart() {
        super.onStart()
        val window: Window? = dialog?.window
        if (isALlHeight()) {
            window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        } else {
            window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        }
        if (isCanCancel()) {
            dialog?.setCancelable(true)
            dialog?.setCanceledOnTouchOutside(true)
        } else {
            dialog?.setCancelable(false)
            dialog?.setCanceledOnTouchOutside(false)
        }
        //显示宽度
        val lp: WindowManager.LayoutParams? = window?.attributes
        lp?.let {
            //没有阴影
            if (isNoShadow()) {
                it.dimAmount = 0f
                it.flags = it.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
            }
            //底部
            if (isBottom()) {
                it.gravity = Gravity.BOTTOM
            }
            window.attributes = lp
        }
    }

    abstract fun initLayout(): View?


    open fun initView() {

    }

    open fun isBottom(): Boolean {
        return false
    }

    open fun isALlHeight(): Boolean {
        return false
    }

    open fun isCanCancel(): Boolean {
        return true
    }

    /**
     * 获取弹窗的动画样式
     *
     * 没有动画  R.style.DialogTheme
     * 从屏幕中心缩放出现   R.style.DialogAnimCenterTheme)
     */
    open fun getAnimStyle(): Int {
        return R.style.DialogTheme
    }


    open fun initDialogStyle(): STYLE {
        return STYLE.STYLE_BOTTOM
    }

    /**
     * false有阴影
     */
    open fun isNoShadow(): Boolean {
        return false
    }


    fun setListener(l: OnDialogListener) {
        mOnDialogListener = l
    }

    fun onClickSure() {
        mOnDialogListener?.onDialogSure()
    }

    fun onClickCancel() {
        mOnDialogListener?.onDialogCancel()
    }

    /**
     * fix Can not perform this action after onSaveInstanceState
     */
    override fun show(manager: FragmentManager, tag: String?) {
        try {
            val filed = DialogFragment::class.java.getDeclaredField("mDismissed")
            filed.isAccessible = true
            filed.set(this, false)

            val showByMe = DialogFragment::class.java.getDeclaredField("mShownByMe")
            showByMe.isAccessible = true
            showByMe.set(this, true)
        } catch (e: NoSuchFieldError) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }


}

interface OnDialogListener {
    fun onDialogSure() {}
    fun onDialogCancel() {}

}